<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

<!-- 用户权限页面布局 -->
<div id="priv-user-layout" class="easyui-layout" style="width:100%;height:100%;">
	<div data-options="region:'north',title:'用户基本信息'" style="width:100%;height:40%;">
    	<!-- 条件搜索栏 -->
		<form id="priv-user-search-form" method="post">
			<label for="name">真实名称:</label> <input class="easyui-validatebox" type="text" name="name" />
			<label for="login_name">登录名称:</label> <input class="easyui-validatebox" type="text" name="login_name" />
			<input type="button" class="icon-search" style="width:50px;" onclick="priv_user.search('priv-user-search-form')"/>
		</form>
		<%--用户列表--%>
		<p><table id="priv-user-datagrid"></table></p>
	</div>      
	<div data-options="region:'center',title:'权限设置'" >
		<!-- 具体用户的权限设置布局 -->
    	<!-- 选项卡 -->
    	<div id="priv-user-tabs" class="easyui-tabs" style="width:100%;height:100%;">   
		    <div title="选择角色" style="padding:20px;display:none;">   
		        <table  id="priv-user-role-datagrid" ></table>
		        <!-- 保存按钮 -->
		        <div style="margin: 30px"></div>
		        <a id="priv-user-role-linkbutton" style="display: none;" onclick="javascript:priv_user.saveRolePrivs()"
		        href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保  存</a>  
		    </div>   
		    <div title="数据权限" style="overflow:auto;padding:20px;display:none;">   
		        <ul id="priv-user-func-tree" data-options="checkbox:true,method:'get',cascadeCheck:false,onlyLeafCheck:true"></ul>
		        <div style="margin: 30px"></div>
		         <!-- 保存按钮 -->
		        <a id="priv-user-func-linkbutton" style="display: none;" onclick="javascript:priv_user.saveFuncPrivs()"
		        href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">保  存</a>     
		    </div>   
		</div>  
	</div>   
</div> 

<script type="text/javascript">
//定义一个包名
var priv_user = {userId:0};

//定义页面初始化方法
priv_user.init = function(){
	$('#priv-user-datagrid').datagrid({    
	    url:'/priv/user/list', 
	    method:'get',
	    columns:[[
	    	{field:'ck',checkbox:true },
	        {field:'id',title:'id',hidden:true},    
	        {field:'name',title:'用户名称'}, 
	        {field:'loginName',title:'登录名称'},
	        {field:'phone',title:'手机号码'},
	        {field:'identity',title:'身份证'}
	    ]],
	    singleSelect:true,
	    selectOnCheck: true,
	    checkOnSelect: true,
	    pagination:true,
	    pageSize:10,
	    onLoadSuccess: function(data){
	    	console.info("#priv-user-datagrid  onLoadSuccess");
	    	priv_user.userId = 0;
	    	$("#priv-user-role-linkbutton").hide();
	    	$('#priv-user-role-datagrid').datagrid('clearChecked');
	    	$("#priv-user-func-linkbutton").hide();
	    }
	});

	//设置单击事件
	$('#priv-user-datagrid').datagrid({
		onSelect: function(rowIndex, rowData){
			//获取用户id
			priv_user.userId = rowData['id'];
			//设置选项卡的信息
			var title = $('#priv-user-tabs').tabs('getSelected').panel('options').title;
			if(title=='选择角色'){
				priv_user.loadRole(priv_user.userId);
				//显示按钮
				$("#priv-user-role-linkbutton").show();
			}else if(title=='数据权限'){
				priv_user.loadFunc(userId);
				$("#priv-user-func-linkbutton").show();
			}
		}
	});
	
	//初始化角色列表
	$('#priv-user-role-datagrid').datagrid({    
	    url:'/priv/user/role/list',
	    queryParams: {
			'userId': priv_user.userId
		},
	    columns:[[
	    	{field:'ck',checkbox:true },
	        {field:'id',title:'id',hidden:true},    
	        {field:'name',title:'角色名称'}
	    ]],
	    singleSelect: false,
	    selectOnCheck: true,
	    checkOnSelect: true,
	    onLoadSuccess: function(data){
	    	console.info("row  onLoadSuccess");
	    	if(data){
	    		$.each(data.rows, function(i, n){
	    			  if(n.checked){
	    				  console.info("row  onLoadSuccess");
	    				  $('#priv-user-role-datagrid').datagrid('checkRow',i);
	    				  $('#priv-user-role-datagrid').datagrid('selectRow',i);
	    				  
	    			  }
	    		});
	    	}
	    }
	});  
	
	
	/* //修改分页属性
	var pager = $('#priv-user-datagrid').datagrid('getPager');
	$(pager).pagination({ 
		layout:['prev','next']
	});
	//获取分页div
	$('#priv-user-layout  div.datagrid-pager.pagination').css("width","198"); */
	
	//初始化选项卡
	$('#priv-user-tabs').tabs({  
	    onSelect:function(title){    
	        //根据选中的用户，加载对应的权限数据
	    	var row = $('#priv-user-datagrid').datagrid('getSelected');
	        if(row==null){
	        	return;
	        }else{
	        	if(title=='选择角色'){
					priv_user.loadRole(row.id);
					$("#priv-user-role-linkbutton").show();
				}else if(title=='数据权限'){
					priv_user.loadFunc(row.id);
					$("#priv-user-func-linkbutton").show();
				}
	        }
	    }    
	});
}

//搜索用户
priv_user.search = function(formId){
	var name = $.trim($('#'+formId+' input[name=name]').val());
	var login_name = $.trim($('#'+formId+' input[name=login_name]').val());
	var params = {};
	if(name!=''){
		params.name = name;
	}
	if(login_name!=''){
		params.login_name = login_name;
	}
	$('#priv-user-datagrid').datagrid('load',params);
	
};

//加载选择角色方法
priv_user.loadRole=function(userId){
	$('#priv-user-role-datagrid').datagrid('load',{'userId': userId});
};

//加载功能数据方法
priv_user.loadFunc=function(userId){
	$('#priv-user-func-tree').tree({    
	    url:'/priv/user/func/tree',
	    queryParams:{'userId':userId}
	}); 
};
//定义保存用户角色权限方法
priv_user.saveRolePrivs=function(){
	
	//获取当前选择的菜单
	var rows = $('#priv-user-role-datagrid').datagrid('getChecked');
	//获取角色ids
	var ids = '';
	for(var index in rows){
		var id = rows[index].id;
		ids += id +",";
	}
	
	//利用ajax向服务器提交数据
	$.ajax({
	   type: "POST",
	   url: "/priv/user/role/add",
	   data: "userId="+priv_user.userId+"&ids="+ids,
	   dataType: "json",
	   success: function(msg){
	     if(msg.code==200){
	    	 $.messager.alert('提示消息','保存用户角色权限成功！','info');
	     }
	   }
	});
}
//定义保存功能权限方法
priv_user.saveFuncPrivs=function(){
	//获取当前选择的功能
	var nodes = $('#priv-user-func-tree').tree('getChecked');
	//获取功能ids
	var ids = '';
	for(var index in nodes){
		var id = nodes[index].id;
		ids += id +",";
	}
	//利用ajax向服务器提交数据
	$.ajax({
	   type: "POST",
	   url: "/priv/user/func/add",
	   data: "userId="+userId+"&ids="+ids,
	   dataType: "json",
	   success: function(msg){
	     if(msg.code==200){
	    	 $.messager.alert('提示消息','保存功能权限成功！','info');
	     }
	   }
	});
}
//页面加载完毕，执行初始化
priv_user.init();
</script> 